import 'dart:async';

import 'package:bilibili_getx_project/ui/pages/functions/video_player_example/video_player_example_logic.dart';
import 'package:get/get.dart';

import 'mini_window_video_player_state.dart';

class MiniWindowVideoPlayerLogic extends GetxController {
  final MiniWindowVideoPlayerState state = MiniWindowVideoPlayerState();

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  hideButtons() {
    state.hideTimer = Timer(Duration(seconds: 3), () {
      state.showButtons = false;
      update();
    });
  }

  startShowButtons() {
    state.showButtons = true;
    state.hideTimer.cancel();
    update();
    if(state.isPlaying) {
      hideButtons();
    }
  }

  closeMiniWindowVideo() {
    state.floating.close();
    VideoPlayerExampleLogic videoPlayerExampleLogic =
        Get.find<VideoPlayerExampleLogic>();
    videoPlayerExampleLogic.state.videoPlayerController =
        state.videoPlayerController;
    videoPlayerExampleLogic.state.openMiniWindow = false;
    videoPlayerExampleLogic.update();
  }

  playOrPauseVideo() {
    if (state.isPlaying) {
      state.videoPlayerController.pause();
      state.isPlaying = false;
      state.hideTimer.cancel();
      state.showButtons = true;
    } else {
      state.videoPlayerController.play();
      state.isPlaying = true;
      startShowButtons();
    }
    update();
  }
}
